﻿using System.Collections.Generic;
using System.Linq;
using Ornament.ImportHelper;

namespace Fee.IO.Importers
{
    public class ProjectImporter<T> : BaseImporter<T> where T : FeeDbContext
    {
        private const int NamePos = 0;

        public ProjectImporter(T context) : base(context, true)
        {
        }

        protected override bool ConverTo(IList<string> rowStringInfo, out ImportResultItem item)
        {
            //项目名称	项目编码	下达年份	首次下达日期	最新下达日期	进度	投资金额（万元）
            //建设期限	建设内容	站点规模数	年度目标	状态	负责人	备注
            item = new ImportResultItem();
            var no = rowStringInfo.ImportString(1, "项目编码", item, true);

            if (item.HasError) return false;


            var project = Context.Projects.FirstOrDefault(f => f.No == no) ??
                          new Project();


            project.Name = rowStringInfo.ImportString(NamePos, "项目名称", item, true);
            if (project.Id == 0)
                project.No = rowStringInfo.ImportString(1, "项目编码", item, false);
            project.CreateYear = rowStringInfo.ImportInt(2, "下达年份", item, false);
            project.FirstOrderTime = rowStringInfo.ImportDatetimeNullable(3, "首次下达日期", item, false);
            project.LastOrderTime = rowStringInfo.ImportDatetimeNullable(4, "最新下达日期", item, false);

            // project.LevelProcess = rowStringInfo.ImportEnum<LevelProcess>(5, "进度", item, true);
            project.Type = rowStringInfo.ImportEnum<ProjectType>(5, "投资类型", item, true);
            project.Amount = rowStringInfo.ImportDecimal(6, "投资金额", item, false);
            project.ConstructionLimitationComment = rowStringInfo.ImportString(7, "建设期限", item, false);
            project.ConstructionContent = rowStringInfo.ImportString(8, "建设内容", item, false);
            project.Station = rowStringInfo.ImportInt(9, "站点规模数", item, false);
            project.YearTargetDesscription = rowStringInfo.ImportString(10, "年度目标", item, false);
            project.Status = rowStringInfo.ImportEnum<ProjectStatus>(11, "状态", item, true);
            project.Owner = rowStringInfo.ImportString(12, "负责人", item, false);
            project.Comment = rowStringInfo.ImportString(13, "备注", item, false);

            if (!item.HasError)
            {
                item.Data.Add(project);
                return true;
            }

            return false;
        }
    }
}